﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCMS.Extensions
{
    /// <summary>
    /// string 扩展类
    /// </summary>
    public static class StringExt
    {
        #region ToString object 类型为空默认值 string
        /// <summary>
        /// object 类型为空默认值 string
        /// </summary>
        /// <param name="str">要处理的字符</param>
        /// <param name="defaultVal">默认值</param>
        /// <returns></returns>
        public static string ToString(this object? str, string defaultVal = "")
        {
            if (str == null || string.IsNullOrWhiteSpace(str.ToString()))
            {
                return defaultVal;
            }

            return str.ToString() ?? "";
        }
        #endregion

        #region ToInt object 类型为空默认值 int 
        /// <summary>
        /// object 类型为空默认值 int 
        /// </summary>		
        /// <param name="str">要处理的字符</param>
        /// <param name="defaultVal">默认值</param>
        /// <returns></returns>
        public static int ToInt(this object? str, int defaultVal = 0)
        {
            if (str == null || string.IsNullOrWhiteSpace(str.ToString()))
            {
                return defaultVal;
            }

            if (int.TryParse(str.ToString(), out int result))
            {
                return result;
            }

            return defaultVal;
        }
        #endregion

        #region ToBool object 类型为空默认值 bool
        /// <summary>
        /// object 类型为空默认值 bool 
        /// </summary>		
        /// <param name="str">要处理的字符</param>
        /// <param name="defaultVal">默认值</param>
        /// <returns></returns>
        public static bool ToBool(this object? str, bool defaultVal = false)
        {
            if (str == null || string.IsNullOrWhiteSpace(str.ToString()))
            {
                return defaultVal;
            }

            if (bool.TryParse(str.ToString(), out bool result))
            {
                return result;
            }

            return defaultVal;
        }
        #endregion

        #region IsSame 判断两个字符串是否完全一样[忽略大小写]
        /// <summary>
        /// 判断两个字符串是否完全一样[忽略大小写]
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public static bool IsSame(this string str1, string str2)
        {
            if (string.IsNullOrWhiteSpace(str1))
            {
                return false;
            }

            return str1.Equals(str2, StringComparison.CurrentCultureIgnoreCase);
        }
        #endregion

        #region InitialSmall 首字母转小写
        /// <summary>
        /// 首字母转小写
        /// </summary>
        /// <param name="str">待处理的字符串</param>
        /// <returns></returns>
        public static string InitialSmall(this string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return str;
            }

            var initial = str.Substring(0, 1).ToLower();

            var str2 = str.Substring(1);
            return initial + str2;
        }
        #endregion

        #region RemoveRepeatSymbol 移除重复符号
        /// <summary>
        /// 移除重复符号
        /// </summary>
        /// <remarks>
        /// 【例如：,1,,2,,,3,，中间出现的重复符号" , "会被移除，只保留一个。,1,2,3,】
        /// </remarks>
        /// <param name="str">待处理的字符串</param>
        /// <param name="symbol">要移除的符号</param>
        /// <returns></returns>
        public static string RemoveRepeatSymbol(this string str, string symbol)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return str;
            }

            var validStr = $"{symbol}{symbol}";

            if (str.IndexOf(validStr) == -1)
            {
                return str;
            }

            string result = str;
            do
            {
                result = result.Replace(validStr, symbol);
            } while (result.IndexOf(validStr) > -1);

            return result;
        }
        #endregion
    }
}
